<template>
    <div class="page">
        <div class="banner">
            <img :src="_oss_ + 'images/homepage/bg.png'">
        </div>

        <div class="main">
            <div class="top">
                <div class="title">用户{{ info.nickname || info.mobile }}的主页</div>

                <div class="btn-group">
                    <div class="btn" :class="info.is_follow ? 'btn-info' : 'btn-primary'"
                        v-if="userStore.userInfo.id != uid" @click="info.is_follow ? onUnFollow() : onFollow()">
                        {{
                            info.is_follow ? "取消关注" : "关注TA"
                        }}
                    </div>
                    <div class="btn btn-default" v-if="userStore.userInfo.id != uid" @click="onDialogue">私信</div>
                    <div class="btn btn-default" @click="onShare">分享</div>
                </div>
            </div>

            <div class="content">
                <div class="left">
                    <iframe :src="`/user_image_2?uid=${uid}`"></iframe>
                    <div class="username">{{ info.nickname || info.mobile }}</div>
                    <div class="personal-signature">
                        个性签名：{{ info.personal_ignature || "-" }}
                    </div>
                    <div class="userinfo">
                        <div class="info-item" @click="onHomepageDialog(1)">
                            <div class="value">{{ info.like_num }}</div>
                            <div class="label">获赞</div>
                        </div>

                        <div class="info-item" @click="onHomepageDialog(2)">
                            <div class="value">{{ info.follow_num }}</div>
                            <div class="label">关注</div>
                        </div>

                        <div class="info-item" @click="onHomepageDialog(3)">
                            <div class="value">{{ info.fans_num }}</div>
                            <div class="label">粉丝</div>
                        </div>
                    </div>
                </div>
                <div class="right">
                    <div class="tabs">
                        <div class="tab-item active">作品</div>
                    </div>

                    <WorksGroupP :list="renderList" v-if="list.length" />
                    <el-empty :image-size="150" description="暂无作品" v-else />

                    <div class="pagination" v-if="list.length">
                        <el-pagination background layout="prev, pager, next, jumper" :total="list.length"
                            :page-size="pagination.size" prev-text="上一页" next-text="下一页" @current-change="onPageChange" />
                    </div>
                </div>
            </div>
        </div>
    </div>

    <DialogDialogueP ref="dialogueDialogRef" />
    <DialogHomepageListUserP ref="homepageListDialogRef" />
</template>
<script setup>
import { GetUserWorkList, GetUserHomepage, Follow, UnFollow } from "@/api";


const route = useRoute();
const dialogStore = useDialogStore();
const userStore = useUserStore();
const uid = route.params.id;
const list = ref([]);
const info = ref({});
const dialogueDialogRef = ref(null);
const homepageListDialogRef = ref(null);
const pagination = reactive({
    size: 6,
    page: 1,
});

const renderList = computed(() => {
    return list.value.slice(
        (pagination.page - 1) * pagination.size,
        pagination.page * pagination.size,
    );
})

const onPageChange = (page) => {
    pagination.page = page;
}

const getList = async () => {
    const res = await GetUserWorkList({ search_uid: uid });
    list.value = res.data;
}

const getInfo = async () => {
    const res = await GetUserHomepage({ search_uid: uid });
    info.value = res.data;
}

const onHomepageDialog = (type) => {
    homepageListDialogRef.value.open(uid, type);
}

const onDialogue = () => {
    if (userStore.isLogin) {
        dialogueDialogRef.value.open(uid);
    } else {
        LoginModal();
    }
}

const onFollow = async () => {
    if (userStore.isLogin) {
        const res = await Follow({ follow_id: uid });
        if (res.code === 200) {
            getInfo();
            Message().success("关注成功");
        } else {
            Message().warning(res.msg);
        }
    } else {
        LoginModal();
    }
}

const onUnFollow = async () => {
    if (userStore.isLogin) {
        const res = await UnFollow({ follow_id: uid });
        if (res.code === 200) {
            getInfo();
            Message().success("取消关注成功");
        } else {
            Message().warning(res.msg);
        }
    } else {
        LoginModal();
    }
}

const onShare = () => {
    dialogStore.isShareTemplateDialogVisible = true;
    dialogStore.shareUrl = location.href;
    dialogStore.shareTitle = "主页链接";
    dialogStore.shareName = info.value.nickname + "的主页";
}

await getInfo();
await getList();
</script>
<style lang="scss" scoped>
.page {
    background-color: #EDF5FA;

    .banner {
        img {
            width: 100%;
            display: block;
        }
    }

    .main {
        position: relative;
        width: 1200px;
        margin: -136px auto 0;
        z-index: 1;

        .top {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 18px;

            .title {
                font-weight: bold;
                font-size: 24px;
                color: #FFFFFF;
            }

            .btn-group {
                display: flex;

                .btn {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    width: 100px;
                    height: 40px;
                    border-radius: 8px;
                    color: #fff;
                    font-size: 18px;
                    margin-left: 24px;
                    cursor: pointer;

                    &:first-of-type {
                        margin-left: 0;
                    }
                }

                .btn-default {
                    border: 1px solid #FFFFFF;
                }

                .btn-primary {
                    background: #FFAD38;
                }

                .btn-info {
                    background: #e7e7e7;
                    color: #666;
                }
            }
        }

        .content {
            display: flex;
            height: 760px;
            background: #FFFFFF;
            box-shadow: 6px 0px 6px 0px rgba(88, 88, 88, 0.03);
            border-radius: 6px;

            .left {
                width: 294px;
                flex-shrink: 0;
                padding: 17px;
                border-right: 1px solid #F1F1F1;

                iframe {
                    display: block;
                    width: 260px;
                    height: 450px;
                    border-radius: 12px;
                    border: none;
                }

                .username {
                    text-align: center;
                    margin-top: 27px;
                    font-weight: bold;
                    font-size: 24px;
                    color: #333333;
                }

                .personal-signature {
                    text-align: center;
                    color: #333;
                    font-size: 16px;
                    margin-top: 23px;
                }

                .userinfo {
                    display: flex;
                    align-items: center;
                    margin-top: 36px;

                    .info-item {
                        text-align: center;
                        flex: 1;
                        cursor: pointer;

                        .value {
                            font-weight: bold;
                            font-size: 16px;
                            color: #333333;
                            line-height: 1em;
                        }

                        .label {
                            font-size: 14px;
                            color: #666;
                            margin-top: 8px;
                            line-height: 1em;
                        }
                    }
                }
            }

            .right {
                flex: 1;
                padding: 12px;

                .tabs {
                    display: flex;

                    .tab-item {
                        padding: 12px 0;
                        font-size: 16px;
                        color: #5085FB;

                        &.active {
                            position: relative;

                            &::after {
                                position: absolute;
                                left: 0;
                                bottom: 0;
                                content: '';
                                display: block;
                                width: 100%;
                                height: 2px;
                                background: #5085FB;
                            }

                        }
                    }
                }

                :deep(.product-list) {
                    .product-item {
                        margin-right: 8px;

                        &:nth-of-type(4n) {
                            margin-right: 8px;
                        }

                        &:nth-of-type(3n) {
                            margin-right: 0;
                        }
                    }
                }

                .pagination {
                    margin-top: 20px;
                    display: flex;
                    justify-content: center;
                }
            }
        }
    }
}
</style>